package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoardService;
import com.example.forum.services.IUserService;
import com.example.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private IUserService userService;

    @Resource
    private IBoardService boardService;

    //查询所有帖子集合
    @Override
    public List<Article> selectAll() {
        //调用DAO
        List<Article> result = articleMapper.selectAll();
        //返回结果
        return result;
    }

    //查询某个版块的帖子列表
    @Override
    public List<Article> selectByBoardId(Long boardId) {
        //非空校验
        if(boardId==null || boardId<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        List<Article> result = articleMapper.selectByBoardId(boardId);
        //返回结果(谁用谁校验)
        return result;
    }

    //发布帖子
    @Override
    public void create(Article article) {
        //非空校验
        if(article==null || article.getBoardId()==null || article.getBoardId()<=0
        || article.getUserId()==null || article.getUserId()<=0
        || StringUtils.isEmpty(article.getTitle())
        || StringUtils.isEmpty(article.getContent())){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        article.setVisitCount(0);  //访问数量
        article.setReplyCount(0);  //回复数量
        article.setLikeCount(0);   //点赞
        article.setState((byte) 0);   //状态
        article.setDeleteState((byte) 0);  //是否删除
        Date date=new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //调用DAO
        int articleRow = articleMapper.insertSelective(article);
        if(articleRow!=1){
            //打印日志
            log.warn(ResultCode.FAILED_CREATE.toString()+"新增帖子失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //更新用户帖子数
        userService.addOneArticleCountById(article.getUserId());
        //更新版块贴子数
        boardService.addOneArticleCountById(article.getBoardId());
        //打印日志
        log.info(ResultCode.SUCCESS.toString()+"发布成功！！！");
    }

    //根据id查询帖子详情
    @Override
    public Article selectById(Long id) {
        //非空校验
        if(id==null || id<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO  根据id查询帖子详情
        Article article = articleMapper.selectById(id);
        //返回结果
        return article;
    }

    @Override
    public void updateVisitCountById(Long id) {
        //非空校验
        if(id==null || id<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        int row = articleMapper.updateVisitCountById(id);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"更新访问次数失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    //编辑更新帖子
    @Override
    public void modify(Long id, String title, String content) {
        //非空校验
        if(id==null || id<=0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造更新对象
        Article article=new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        //更新时间
        Date date=new Date();
        article.setUpdateTime(date);
        //调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"编辑帖子失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    //传入帖子对象更新帖子
    @Override
    public void updateById(Article article) {
        //非空校验
        if(article==null || article.getId()==null || article.getId()<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //更新时间  保证更新操作永远都是正确的
        //article.setUpdateTime(new Date());
        //调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"修改帖子失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    //根据用户id查询帖子列表
    @Override
    public List<Article> selectByUserId(Long userId) {
        //非空校验
        if(userId==null || userId<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        List<Article> articles = articleMapper.selectByUserId(userId);
        //返回结果
        return articles;
    }

    @Override
    public void delete(Article article) {
        //非空校验
        if(article==null || article.getBoardId()==null || article.getBoardId()<=0
        || article.getUserId()==null || article.getUserId()<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //判断帖子是否已删除
        if(article.getDeleteState()==1){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //将删除状态改为已删除
        article.setDeleteState((byte) 1);
        //更新用户的帖子数
        userService.subOneArticleCountById(article.getUserId());
        //更新版块的帖子数
        boardService.subOneArticleCountById(article.getBoardId());
        //调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"删除帖子失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void thumbsUpById(Long id) {
        // ⾮空校验
        if (id==null || id<=0) {
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询帖⼦信息
        Article article = selectById(id);
        if (article == null || article.getState() == 1 || article.getDeleteState() == 1) {
            // FAILED_NOT_EXISTS
            // 记录⽇志
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 构造更新对象
        Article update = new Article();
        update.setId(article.getId());
        update.setLikeCount(article.getLikeCount() + 1);
        // 更新数据库
        int row = articleMapper.updateByPrimaryKeySelective(update);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"点赞帖子失败！！！");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
}
